package com.system.manager.modules.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.manager.common.entity.PageDTO;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.service.IUserService;
import com.system.manager.modules.risk.entity.StaffStation;
import com.system.manager.modules.risk.entity.TbFile;
import com.system.manager.modules.risk.entity.TbFileStaffstation;
import com.system.manager.modules.risk.mapper.TbFileStaffstationMapper;
import com.system.manager.modules.risk.service.ITbFileStaffstationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.manager.modules.risk.service.ITbTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-24
 */
@Service
public class TbFileStaffstationServiceImpl extends ServiceImpl<TbFileStaffstationMapper, TbFileStaffstation> implements ITbFileStaffstationService {

    @Autowired
    private TbFileStaffstationMapper tbFileStaffstationMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITbTaskService iTbTaskService;
    //当推送文件的时候，将部门里的所有成员都加上该文件任务
    @Override
    public void insertFileStaffStaion(List<StaffStation> staffStationList, TbFile file) {
        for (StaffStation staffStation : staffStationList) {
            TbFileStaffstation fileStaffstation = new TbFileStaffstation();
            //获取用户Id
            Integer staffStationId = staffStation.getUId();

            //获取文件id
            Integer fileId = file.getFileId();
            //文件名
            String fileName = file.getFileName();
            //文件路径
            String filePath = file.getFile();
            fileStaffstation.setUserId(staffStationId);
            fileStaffstation.setFileId(fileId);
            fileStaffstation.setFileName(fileName);
            fileStaffstation.setFile(filePath);
            fileStaffstation.setState(0);
            tbFileStaffstationMapper.insert(fileStaffstation);
        }

    }

    //根据用户id查询所需要的文件

    @Override
    public PageDTO<TbFileStaffstation> selection(Integer page, Integer size, Subject subject) {
        User byId = userService.getById(subject.getId());
        Integer userId = byId.getUserId();



        IPage<TbFileStaffstation> iPage = new Page<>(page,size);


        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        page(iPage,wrapper);

        PageDTO<TbFileStaffstation> pageDTO = new PageDTO<>();

        pageDTO.setData(iPage.getRecords());
        pageDTO.setCount(iPage.getTotal());
        return pageDTO;
    }

    /*
    修改状态
     */
    @Override
    public void updateState(Subject subject, Integer fileId) {
        //获取用户id
        User byId = userService.getById(subject.getId());
        Integer userId = byId.getUserId();

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("file_id",fileId);

        //修改状态
        TbFileStaffstation fileStaffstation = tbFileStaffstationMapper.selectOne(wrapper);
        fileStaffstation.setState(1);

        tbFileStaffstationMapper.updateById(fileStaffstation);

        //修改评估表的成绩
        iTbTaskService.updateAviScore(userId);
        iTbTaskService.updateFileScore(userId);

    }

    /*
    根据用户id查询用户完成情况
    需要完成的情况
     */

    @Override
    public List<TbFileStaffstation> selectOkByStaffStation(Integer userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        List<TbFileStaffstation> list = tbFileStaffstationMapper.selectList(wrapper);
        return list;
    }
}
